﻿[
{
    "name": "Application Data Folders",
    "desc": "Provides access to the app data store. Application data consists of files and settings that are either local, roaming, or temporary.",
    "source": function () {
        var ad = Windows.Storage.ApplicationData.current;
        console.log("Application Data Folders: version", ad.version, {
            "Local Folder": ad.localFolder.path,
            "Roaming Folder": ad.roamingFolder.path,
            "Temp Folder": ad.temporaryFolder.path
        });
    }
},
{
    "name": "Application Data",
    "desc": "Provides access to the app data store. Application data consists of files and settings that are either local, roaming, or temporary.",
    "source": function () {
        var putValues = function (container) {
            container.values.insert("key-" + new Date().getTime(), new Date().toString());
            console.log("Added value", container.name);
            var c = container.createContainer("container-" + new Date().getTime(), Windows.Storage.ApplicationDataCreateDisposition.always);
            c.values.insert("key-" + new Date().getTime(), new Date().toString());
        }

        var getValues = function (container) {
            var locality = ["Local", "Roaming", "Temporary"];
            var result = [];
            if (typeof container.values !== "undefined") {
                for (var i = container.values.first(); i.hasCurrent; i.moveNext()) {
                    result.push(i.current.key + ":" + i.current.value);
                }
            }
            var inner = [];
            if (typeof container.containers !== "undefined") {
                for (var i = container.containers.first(); i.hasCurrent; i.moveNext()) {
                    inner.push(getValues(i.current.value));
                }
            }
            return {
                "Name": container.name,
                "Type": locality[container.locality],
                "Values": result,
                "Inner Containers": inner
            }
        }

        console.log("Local");
        putValues(Windows.Storage.ApplicationData.current.localSettings);
        console.log(getValues(Windows.Storage.ApplicationData.current.localSettings));

        console.log("Roaming");
        putValues(Windows.Storage.ApplicationData.current.roamingSettings);
        console.log(getValues(Windows.Storage.ApplicationData.current.roamingSettings));
    }
},

{
    "name": "Compress a file",
    "desc": "Compresses a file",
    "source": function () {
        var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
        fileOpenPicker.fileTypeFilter.append(".txt");
        fileOpenPicker.pickSingleFileAsync().then(function (file) {
            file.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (iStream) {
                var fileSavePicker = new Windows.Storage.Pickers.FileSavePicker();
                fileSavePicker.fileTypeChoices.insert("Text", [".txt"]);
                fileSavePicker.pickSaveFileAsync().then(function (file) {
                    file.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (oStream) {
                        var dataReader = new Windows.Storage.Streams.DataReader(iStream.getInputStreamAt(0));
                        var data = [];
                        dataReader.loadAsync(1000).then(function (count) {
                            var compressor = new Windows.Storage.Compression.Compressor(oStream.getOutputStreamAt(0), Windows.Storage.Compression.CompressAlgorithm.mszip);
                            compressor.writeAsync(dataReader.readBuffer(count)).then(function () {
                                compressor.finishAsync().then(function () {
                                    compressor.flushAsync();
                                    console.log("Compression compelted in file ", file.path);
                                });
                            });
                        });
                    });
                });
            });
        });
    }
},

{
    "name": "Decompress a file",
    "desc": "Decompress a compressed file and print the contents",
    "source": function () {
        var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
        fileOpenPicker.fileTypeFilter.append(".txt");
        fileOpenPicker.pickSingleFileAsync().then(function (file) {
            file.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (iStream) {
                var decompressor = new Windows.Storage.Compression.Decompressor(iStream.getInputStreamAt(0));
                var dataReader = new Windows.Storage.Streams.DataReader(iStream.getInputStreamAt(0));
                var bufferReader = new Windows.Storage.Streams.DataReader(iStream.getInputStreamAt(0));
                var result = [];
                dataReader.loadAsync(10000).then(function (count) {
                    var buffer = dataReader.readBuffer(count);
                    decompressor.readAsync(buffer, count, Windows.Storage.Streams.InputStreamOptions.readAhead).then(function (result) {
                        console.log(Windows.Storage.Streams.DataReader.fromBuffer(buffer).readString(buffer.length));
                    });
                });
            });
        });
    }
},
{
    "name": "Downloads Folder",
    "desc": "Created a file in the downloads folder",
    "source": function () {
        Windows.Storage.DownloadsFolder.createFileAsync("sample.txt").then(function (file) {
            file.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (stream) {
                var dataWriter = new Windows.Storage.Streams.DataWriter(stream.getOutputStreamAt(0));
                dataWriter.writeString("Hello World " + new Date());
                dataWriter.storeAsync().then(function () {
                    console.log("Contents written to file");
                });
            }, function (e) {
                console.error("Error", e.message);
            });
        });
    }
},
{
    "name": "Known Folders",
    "desc": "Provides access to folders of user content (like pictures, documents, or videos) that is shared across applications.",
    "source": function () {
        function show(e) {
            var knownFolders = Windows.Storage.KnownFolders;
            try {
                console.log(e, knownFolders[e].path);
            } catch (e) {
                console.error(e.message);
            }
        }

        show("documentsLibrary");
        show("mediaServerDevices");
        show("musicLibrary");
        show("picturesLibrary");
        show("removableDevices");
        show("videosLibrary");
        show("homeGroup");
    }
},
{
    "name": "Add Item to Access Cache",
    "desc": "Add an item to application access cache",
    "source": function () {
        var storageItemMostRecentlyUsedList = Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList;
        storageItemMostRecentlyUsedList.add(Windows.System.UserProfile.UserInformation.getUserImage(Windows.System.UserProfile.UserImageKind.smallBitmap));
        console.log("Added 2 items to the list of access cache files");
    }
},
{
    "name": "View Access Cache",
    "desc": "Enables an app to retrieve storage items from the access list or the most recently used (MRU) list.",
    "source": function () {
        var storageItemMostRecentlyUsedList = Windows.Storage.AccessCache.StorageApplicationPermissions.mostRecentlyUsedList;
        console.log("Items in Storage List", storageItemMostRecentlyUsedList.entries.size);
        storageItemMostRecentlyUsedList.entries.forEach(function (a) {
            console.log(a);
        });
    }
}
]